//
//  ResultViewController.m
//  TestAge2016(第六组)
//
//  Created by student on 16/4/11.
//  Copyright © 2016年 wk. All rights reserved.
//

#import "ResultViewController.h"
#import "NetManager.h"
#import "AgeInfoView.h"
@interface ResultViewController ()
@property (weak, nonatomic) IBOutlet UIActivityIndicatorView *activityView;
@property (weak, nonatomic) IBOutlet UILabel *analysisLabel;
@property (weak, nonatomic) IBOutlet UIImageView *resultImageView;


@end

@implementation ResultViewController

- (void)viewDidLoad {
    [super viewDidLoad];

    self.resultImageView.image = [UIImage imageWithData:self.imageData];
    [self.activityView startAnimating];
    
    [NetManager testAgeByImageData:self.imageData
                      successBlock:^(NSArray *ageInfo) {
                          // 菊花停止运转，并且隐藏
                          [self.activityView stopAnimating];
                          self.activityView.hidden = YES;
                          self.analysisLabel.hidden = YES;
                          
                          for (NSDictionary *tmpDic in ageInfo) {
                              
                              NSDictionary *rectangleDic = tmpDic[@"faceRectangle"];
                              NSDictionary *attributesDic = tmpDic[@"attributes"];
                              CGFloat x = [rectangleDic[@"left"] floatValue];
                              CGFloat y = [rectangleDic[@"top"] floatValue];
                              CGFloat width = [rectangleDic[@"width"] floatValue];
                              CGFloat height = [rectangleDic[@"height"] floatValue];
                              [self creatInfoViewWithFaceRectangle:CGRectMake(x, y, width, height) age:[attributesDic[@"age"] intValue] gender:[attributesDic[@"gender"] isEqualToString:@"Male"]];
                          }
                      } failBlock:^(NSError *error) {
                          
                      }];
    
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
       
}

- (IBAction)tryAgainBtnPressed:(UIButton *)sender {
    
    
    [self dismissViewControllerAnimated:YES completion:nil];
}

- (IBAction)saveBtnPressed:(UIButton *)sender {
    
    
    
}

- (IBAction)shareBtnPressed:(UIButton *)sender {
    
    
}

- (void)creatInfoViewWithFaceRectangle:(CGRect)faceRectangle
                                   age:(int)age
                                gender:(BOOL)male
{
    // 画白框
    UIView *faceView = [[UIView alloc] init];
    faceView.backgroundColor = [UIColor clearColor];
    faceView.layer.borderColor = [UIColor whiteColor].CGColor;
    faceView.layer.borderWidth = 2.0f;
    
    UIImage *image = self.resultImageView.image;
    CGFloat imageWidth = image.size.width;
    CGFloat imageHeight = image.size.height;
    
    CGFloat imageViewWidth = self.resultImageView.frame.size.width;
    CGFloat imageViewHeight = self.resultImageView.frame.size.height;
    
    CGPoint imageViewCenter = self.resultImageView.center;
    // 白框的尺寸位置
    CGFloat width;
    CGFloat height;
    CGFloat centerX;
    CGFloat centerY;
    
    CGFloat scale = imageViewWidth/imageWidth;
    
    width = faceRectangle.size.width*scale;
    height = faceRectangle.size.height*scale;
    // 白框的中心点的偏移
    CGFloat centerXDelta = faceRectangle.origin.x + faceRectangle.size.width/2.0f - imageWidth/2.0f;
    CGFloat centerYDelata = faceRectangle.origin.y + faceRectangle.size.height/2.0f - imageHeight/2.0f;
    
    centerX = imageViewCenter.x + centerXDelta*scale;
    centerY = imageViewCenter.y + centerYDelata*scale;
    
    faceView.frame = CGRectMake(0, 0, width, height);
    faceView.center = CGPointMake(centerX, centerY);
    
    [self.view addSubview:faceView];
    AgeInfoView *infoView = [[AgeInfoView alloc] init];
    infoView.frame = CGRectMake(0, 0, 100, 100);
    [self.view addSubview:infoView];
    infoView.age = age;
    infoView.male = male;

    infoView.center = CGPointMake(centerX, centerY-height/2.0f-infoView.frame.size.height/2.0f);
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
